﻿using Microsoft.AspNetCore.Mvc;
using System.Text;

namespace NetCore6_MVC.Controllers
{
    public class GCDemoController : Controller
    {
        private static List<string> StringList = new List<string>();
        private static long TotalCount = 0;

        /// <summary>
        /// 1就持续增加   0就清除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        //[Route("GC")]
        public IActionResult TestGCKeep(int id)
        {
            TotalCount += 1;

            Console.WriteLine($"This is {TotalCount} Invoke TestGCKeep1 id={id}");

            #region MyRegion
            if (id == 0)
            {
                StringList.Clear();
                return new JsonResult(new
                {
                    Result = true,
                    Value = new
                    {
                        Heap = GC.GetTotalMemory(false) / 1024 / 1024,
                        TotalCount = TotalCount
                    },
                    Message = "Clear All MemoryUse"
                });
            }
            #endregion
            StringBuilder stringBuilder = new StringBuilder();
            for (int i = 0; i < 3800 * 20; i++)
            {
                stringBuilder.Append("我深深的理解，耗费了多少时间，战胜了多少困难，你才取得眼前的成绩。而未来的征程里，将还有更多的艰难险阻等着你去战胜，去征服!请你相信，在你追求、拼搏和苦干的过程中，朝夕教育将永远站在你的身旁！");
            }
            string str = stringBuilder.ToString();
            StringList.Add(str);

            return new JsonResult(new
            {
                Result = true,
                Value = new
                {
                    Heap = GC.GetTotalMemory(false) / 1024 / 1024,
                    TotalCount = TotalCount
                },
                Message = $"第{TotalCount}次占据内存"
            });
        }

    }
}
